package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfx.entity.CmsContent;
import com.wfx.entity.PageDTO;
import com.wfx.entity.PageResult;
import com.wfx.entity.Result;
import com.wfx.mapper.CmsContentMapper;
import com.wfx.service.ICmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-23
 */
@Service
public class CmsContentServiceImpl extends ServiceImpl<CmsContentMapper, CmsContent> implements ICmsContentService {

    @Autowired
    private  CmsContentMapper cmsContentMapper;

    @Override
    public PageResult<CmsContent> search(PageDTO<CmsContent> pageDTO) {
        IPage<CmsContent> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper(pageDTO.getData());
        IPage iPageInfo = cmsContentMapper.selectPage(iPage, queryWrapper);
        PageResult<CmsContent> pageResult = new PageResult<>();
        pageResult.setMsg("甲方妈妈，别搞我了");
        pageResult.setSuccess(true);
        pageResult.setPages(iPageInfo.getPages());
        pageResult.setTotal(iPageInfo.getTotal());
        pageResult.setData(iPageInfo.getRecords());

        return pageResult;
     }

    @Override
    public Result upAndAdd(CmsContent cmsContent) {
        if(cmsContent.getId()==null){ //判断id是否存在 ，为空就是添加
            int insert = cmsContentMapper.insert(cmsContent);
            return new Result(insert>0,"添加成功");
        }else {  //否则就是修改
            int i = cmsContentMapper.updateById(cmsContent);
            return new Result(i>0,"修改成功");

        }


    }

    @Override
    public Result del(List<String> ids) {
        int i = cmsContentMapper.deleteBatchIds(ids);
        return new Result(i>0?true:false,i>0?"删除成功":"删除失败");

    }
}
